//
//  SABatchPurchaseDetailCell.m
//  saasRetail
//
//  Created by mac on 2022/10/10.
//

#import "SABatchPurchaseDetailCell.h"
#import "SAOcrScanResultModel.h"

@interface SABatchPurchaseDetailCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *colourTf;
@property (nonatomic, strong) UITextField *sizeTf;
@property (nonatomic, strong) UITextField *numTf;
@property (nonatomic, strong) UIView *numView;

@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) UIButton *minusBtn;

@end

@implementation SABatchPurchaseDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    
    if (textField == self.numTf) {
        if (textField.text.length > 3) {
            return NO;
        }
    }
    
    if (textField.text.length > 7) {
        return NO;
    }
    
    if (textField == self.numTf) {
        return [self validateNumber:string];
    }
    
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField *)textField {
    if (self.colourTf == textField) {
        self.model.colorName = self.colourTf.text;
    }else if (self.sizeTf == textField) {
        self.model.sizeName = self.sizeTf.text;
    }else if (self.numTf == textField) {
        self.model.count = self.numTf.text;
        !self.changeNumBlock ?: self.changeNumBlock();
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.colourTf) {
        self.colourTf.layer.borderColor = kRedColor.CGColor;
    }else if (textField == self.sizeTf) {
        self.sizeTf.layer.borderColor = kRedColor.CGColor;
    }else if (textField == self.numTf) {
        self.numView.layer.borderColor = kRedColor.CGColor;
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField == self.colourTf) {
        self.colourTf.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    }else if (textField == self.sizeTf) {
        self.sizeTf.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    }else if (textField == self.numTf) {
        self.numView.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    }
    return YES;
}

#pragma mark - setModel
-(void)setModel:(OcrSkuModel *)model {
    _model = model;
    self.colourTf.text = model.colorName;
    self.sizeTf.text = model.sizeName;
    self.numTf.text = [NSString stringWithFormat:@"%zd",[model.count integerValue]];
}

#pragma mark - event
-(void)clickedMinusBtn {
    [self endEditing:YES];
    NSInteger num = [self.model.count integerValue] - 1;
    if (num < 0) {
        num = 0;
    }
    self.model.count = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.count;
    !self.changeNumBlock ?: self.changeNumBlock();
}

-(void)clickedAddBtn {
    [self endEditing:YES];
    NSInteger num = [self.model.count integerValue] + 1;
    if (num > 9999) {
        num = 9999;
    }
    self.model.count = [NSString stringWithFormat:@"%zd",num];
    self.numTf.text = self.model.count;
    !self.changeNumBlock ?: self.changeNumBlock();
}

#pragma mark - initUI
-(void)initUI {
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 1 * kScreenScale, kScreenWidth, 1)];
    line.backgroundColor = kLineColor;
    [self.contentView addSubview:line];
    
    UITextField *colourTf = [[UITextField alloc] initWithFrame:SARectMake(10, 6, 105, 30)];
    colourTf.font = kFont(15);
    colourTf.textColor = kBlackColor;
    colourTf.textAlignment = NSTextAlignmentCenter;
    colourTf.layer.cornerRadius = kScaleNum(5);
    colourTf.layer.masksToBounds = YES;
    colourTf.layer.borderWidth = kScaleNum(1);
    colourTf.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    colourTf.delegate = self;
    [colourTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:colourTf];
    self.colourTf = colourTf;
    
    UITextField *sizeTf = [[UITextField alloc] initWithFrame:SARectMake(135, 6, 105, 30)];
    sizeTf.font = kFont(15);
    sizeTf.textColor = kBlackColor;
    sizeTf.textAlignment = NSTextAlignmentCenter;
    sizeTf.layer.cornerRadius = kScaleNum(5);
    sizeTf.layer.masksToBounds = YES;
    sizeTf.layer.borderWidth = kScaleNum(1);
    sizeTf.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    sizeTf.delegate = self;
    [sizeTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:sizeTf];
    self.sizeTf = sizeTf;
    
    UIView *numView = [[UIView alloc] initWithFrame:SARectMake(260, 6, 105, 30)];
    numView.layer.cornerRadius = kScaleNum(5);
    numView.layer.masksToBounds = YES;
    numView.layer.borderWidth = kScaleNum(1);
    numView.layer.borderColor = kColor(120, 120, 120, 1).CGColor;
    [self.contentView addSubview:numView];
    self.numView = numView;
    
    UITextField *numTf = [[UITextField alloc] initWithFrame:SARectMake(290, 1, 45, 40)];
    numTf.font = kFont(15);
    numTf.textColor = kBlackColor;
    numTf.textAlignment = NSTextAlignmentCenter;
    numTf.delegate = self;
    [numTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:numTf];
    numTf.keyboardType = UIKeyboardTypeNumberPad;
    self.numTf = numTf;
    
    [self.contentView addSubview:self.minusBtn];
    [self.contentView addSubview:self.addBtn];
}

-(UIButton *)minusBtn {
    if (!_minusBtn) {
        _minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_minusBtn setImage:[UIImage imageNamed:@"shop_cutBtn"] forState:UIControlStateNormal];
        _minusBtn.frame = SARectMake(265, 0, 30, 40);
        _minusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_minusBtn addTarget:self action:@selector(clickedMinusBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _minusBtn;
}

-(UIButton *)addBtn {
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"shop_addBtn"] forState:UIControlStateNormal];
        _addBtn.frame = SARectMake(330, 0, 30, 40);
        _addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_addBtn addTarget:self action:@selector(clickedAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

@end
